package com.alex;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.io.FilenameUtils ;
import org.apache.commons.io.comparator.LastModifiedFileComparator;

public class App {

	/**
	 * @param args
	 */

	public static void main(String[] args) {
		
		// TODO Auto-generated method stub
		ArrayList<File> files = new ArrayList<File>();

		// Get the source path
		File sourcePath = new File (args[0]) ;

		// Get the target path
		String targetPath = new String(args[1]);

		// Get the radical
		String radical = new String(args[2]);

		// Get the first number
		long startNb = Integer.parseInt(args[3]);

		// Parse the sourcePath to retrieve all files
		files = Parse(sourcePath);

		// Give the number of files to process
		System.out.println ( "Number of files to process : " + files.size()) ;
		
		// Trier les photos
		Collections.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR) ;
		
		// Make the copy
		makeCopy ( files, targetPath, radical, startNb ) ;
	}
	
	private static void makeCopy(ArrayList<File> files, String targetPath, String radical, long startNb ) {
		// TODO Auto-generated method stub
		long number = startNb ;
		int cpt = 0 ;
		String year = new String() ;
		byte [] buf = new byte[1024] ;
		Date dt = new Date() ;
		HashSet<String> years = new HashSet<String>() ;
		PrintStream ps = null ;
		String targetFileName = new String () ;
		
		System.out.println ( "Number of files to process : " + files.size()) ;
		
		try {
			ps = new PrintStream(targetPath+"File.log");
			ps.println ( "Number of file to process" + files.size()) ;
			ps.flush();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		for ( Iterator<File> f = files.iterator() ; f.hasNext() ; ) {
			// Create the target directory for the given file
			// and  build the target filename
			// TODO : Change the method to retrieve the year of the file
			File file = f.next() ;
			
			dt = new Date(file.lastModified()) ;
			year = String.format("%4s", dt.getYear()+1900 ) ;			
			targetFileName = targetPath + File.separator + year + File.separator + radical + String.format( "%010d", number) + "." + FilenameUtils.getExtension(file.getName()) ;
			
			System.out.println ( file.getAbsolutePath() + " copied to " + targetFileName) ;
			ps.flush() ;
			if ( cpt > files.size() / 10 ) {
				System.out.print ("X") ;
				cpt = 0 ;
			}
			else {
				cpt ++ ;
			}
			
			if ( years.add(year) == true ) {
				File path = new File ( targetPath + File.separator + year) ;
				path.mkdirs() ;
			}
			
			try {
				FileInputStream fi = new FileInputStream(file) ;
				FileOutputStream fo = new FileOutputStream(targetFileName) ;
				
				while ( fi.read(buf)  > 0 ) {
					fo.write(buf) ;
				}
				number++ ;
				fi.close () ;
				fo.close() ;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		System.out.println ( "Number of processed files : " + (number - startNb) ) ;
	}

	private static ArrayList<File> Parse(File sourcePath) {
		// TODO Auto-generated method stub
		ArrayList<File> files = new ArrayList<File>();
		ArrayList<File> dirs = new ArrayList<File>();
		
		for (int i = 0; i < sourcePath.listFiles().length; i++) {
			
			if (sourcePath.listFiles() [i].isFile() == true ) {
				files.add((File)sourcePath.listFiles() [i]);
				
			}
			else {
				dirs.add((File)sourcePath.listFiles() [i]);
			}
		}

		for (Iterator<File> dir = dirs.iterator(); dir.hasNext();) {
			files.addAll(Parse(dir.next()));
		}
		
		
		return files;
	}

}
